<?php

namespace App\Events;

use App\Model\Eeoapi\Eeoteacher;
use App\Model\Foundation\Teacher;
use App\Model\System\Eeofile;

class TeacherEvent extends Event
{

    # 教师 calssin 账号
    public $teacherAccount;
    public $password;
    # 教师名字
    public $teacherName;
    # 教师头像图片地址
    public $Filedata;
    # eeo stid
    public $stid;
    public $teacher;

    /**
     * TeacherEvent constructor.
     * @param $action
     * @param Teacher $teacher
     */
    public function __construct($action, Teacher $teacher, $password=null)
    {
        parent::__construct();
        $this->action = $action;
        $this->teacher = $teacher;
        $this->password = $password;
        $this->teacherName = $teacher->lastname;
        $this->teacherAccount = $teacher->ClassInNo;

        if($teacher->photo){
            $photo = $teacher->photo;
            $photo = str_replace('/api/file/', '', $photo);
            $photo = str_replace('/api/file', '', $photo);
            $photo = str_replace('?u=', '', $photo);
            $photo = Eeofile::getLocalPath($photo);
            if(file_exists($photo))
                $this->Filedata = $photo;
        }

        $obj = Eeoteacher::where('teacheruid', $teacher->teacheruid)->first();
        if($obj){
            $this->stid = $obj->stid;
        }
    }

    /**
     * 设置eeo 返回 stid
     * @param $stid
     */
    public function setStid($stid)
    {
        $this->stid = $stid;
    }


    /**
     * 自动创建映射关系
     */
    public function __destruct()
    {
        $fileds = ['teacheruid'=>$this->teacher->teacheruid, 'stid'=>$this->stid];
        Eeoteacher::firstOrCreate($fileds);
    }

}
